﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Web;
using Newtonsoft;
using HtmlAgilityPack;
using WorldExchange;
using DatabaseService;
using Newtonsoft.Json;
namespace WorldGoldenPrice
{
    public class GoldenPriceEasy : GoldenPrice
    {
        public Double OildPara;       
        public override void getPrice()
        {
            this.VnBuy = "";
            this.VnSell = "";
            this.WorldBuy = "";
            this.WorldSell = "";
            this.WorldSell = "";
            this.LinkService = ConfigurationSettings.AppSettings["GoldPrice_WebService_Easy"].ToString().Trim();
            OildPara = Convert.ToDouble(ConfigurationSettings.AppSettings["OildPara"].ToString().Trim());
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                 string data = WebTool.PostData2(this.LinkService, "curr_pairs=EURUSD-GBPUSD-USDJPY-USDCHF-USDCAD-AUDUSD-EURGBP-EURJPY-GBPJPY-AUDJPY-USDNOK-XAUUSD-XAGUSD-OILUSD&curr_vals=1.3346:1.5861:88.55:0.9286:0.9914:1.0552:0.8413:118.2:140.45:93.43:5.5452:1694.08:32.2789:96.83");
             
                if (data != null)
                {
                    int beginXau = data.IndexOf("XAUUSD");
                    string AfterXau = data.Substring(beginXau + 7);
                    string[] aarAfterxau = AfterXau.Split(new char[] { ':' });
                    if (aarAfterxau.Length > 0)
                    {
                        this.WorldBuy = aarAfterxau[0];
                        this.WorldSell = aarAfterxau[1];
                    }
                }
                if (data != null)
                {
                    int beginOild = data.IndexOf("OILUSD");
                    string AfterOild = data.Substring(beginOild + 7);
                    string[] aarAfterOil = AfterOild.Split(new char[] { ':' });
                    if (aarAfterOil.Length > 0)
                    {
                        this.OilBuy = aarAfterOil[0];
                        this.OilSell = aarAfterOil[1];
                    }
                }

                
                
                //this.OilBuy = (Convert.ToSingle(xmlNode.SelectNodes("//OILUSD")[0].InnerText)).ToString("0.00");
                //this.OilSell = (Convert.ToSingle(xmlNode.SelectNodes("//OILUSD")[1].InnerText)).ToString("0.00");

                //Quy đổi ra Việt nam đồng
                Single wBuy = Convert.ToSingle(this.WorldBuy);
                Single wSell = Convert.ToSingle(this.WorldSell);


                Exchange_Factory TGNT = AccessDB.getExchangeInfo();
                Single exRate = Convert.ToSingle(TGNT.USD.Ban);

                this.VnBuy = (((wBuy + 0) * 1.0 * 1.205656 * exRate + 0) / 1000).ToString("#,###");
                this.VnSell = (((wSell + 0) * 1.0 * 1.205656 * exRate + 0) / 1000).ToString("#,###");

           }
            catch (Exception ex)
            {
                TLog.WriteErr("GoldenPriceEasy : getPrice", ex.ToString());
                
            }
            setNewValue();

            
        }

    }
}
